options ls = 100 ps = 80 probsig=2;

data fuzzy1;  informat code $25.;informat bcode $20.;
   infile 'c:/My Documents/data/scAT.6/AAT6b.dat' delimiter='09'x;
   input date    time  $  build  sub   code    $   block   trial  lat     resp  $  corr  trialitm    bcode $;
data fuzzy2;  informat code $25.;informat bcode $20.;
   infile 'c:/My Documents/data/scAT.6/AAT6g.dat' delimiter='09'x;
   input date    time  $  build  sub   code    $   block   trial  lat     resp  $  corr  trialitm    bcode $;
data fuzzy3;  informat code $25.;informat bcode $20.;
   infile 'c:/My Documents/data/scAT.6/AAT6f.dat' delimiter='09'x;
   input date    time  $  build  sub   code    $   block   trial  lat     resp  $  corr  trialitm    bcode $;
data temp; set fuzzy1 fuzzy2 fuzzy3;

if block in (1,2) then delete;if trial < 19 then delete;if bcode = 'bcode' then delete;
if sub in (22, 58) then delete;  

if code in ('badbackground_false1000', 'goodbackground_false1000', 
            'fruit_true1000', 'bugs_true1000') then delete;

if code in ('bugs_false833') then code = 'badbackground_false833';
if code in ( 'good_false833', 'bad_false833', 'goodbackground_false833', 'badbackground_false833',
             'good_false666', 'bad_false666', 'goodbackground_false666', 'badbackground_false666')
then item = 0;else item = 1;

if corr = 0 and item = 0 then response = 'corrrej';if corr = 0 and item = 1 then response = 'miss';
if corr = 1 and item = 0 then response = 'falsalr';if corr = 1 and item = 1 then response = 'hit';

proc means n mean std; class bcode code; var corr; run;

proc sort; by sub bcode response; run;
proc means noprint data=temp n; by sub bcode response; var lat; output out=means  mean=speed; run;
proc transpose data=means prefix=speed name=name out=spdmeans; by sub bcode response;run;
proc sort; by name; run;
data prac;  set spdmeans; if name ^= '_FREQ_' then delete;
   /*if speed1 < 0 then speed1 = 0;*/
   if response = 'corrrej' then do;
      if speed1 = 20 then do; response = 'falsalr'; speed1 = 0.35; end;
      else delete; end;
   if response = 'miss' then do;
      if speed1 = 20 then do; response = 'hit'; speed1 = 0.35; end;
      else delete; end;
   if response = 'hit' and speed1 = 20 then speed1 = 19.65;
   if response = 'falsalr' and speed1 = 20 then speed1 = 19.65;
proc sort; by sub;
proc transpose data=prac prefix=freq name=name out=freq; by sub; run;
proc sort; by name; run;
data prac; set freq;
   BFbgbd66 = probit(freq1/20); BHbgbd66 = probit(freq2/20);
   BFbgbd83 = probit(freq3/20); BHbgbd83 = probit(freq4/20);
   BFbggd66 = probit(freq5/20); BHbggd66 = probit(freq6/20);
   BFbggd83 = probit(freq7/20); BHbggd83 = probit(freq8/20);
   BFftbd66 = probit(freq9/20); BHftbd66 = probit(freq10/20);
   BFftbd83 = probit(freq11/20); BHftbd83 = probit(freq12/20);
   BFftgd66 = probit(freq13/20); BHftgd66 = probit(freq14/20);
   BFftgd83 = probit(freq15/20); BHftgd83 = probit(freq16/20);
   gFbgbd66 = probit(freq17/20); gHbgbd66 = probit(freq18/20);
   gFbgbd83 = probit(freq19/20); gHbgbd83 = probit(freq20/20);
   gFbggd66 = probit(freq21/20); gHbggd66 = probit(freq22/20);
   gFbggd83 = probit(freq23/20); gHbggd83 = probit(freq24/20);
   gFftbd66 = probit(freq25/20); gHftbd66 = probit(freq26/20);
   gFftbd83 = probit(freq27/20); gHftbd83 = probit(freq28/20);
   gFftgd66 = probit(freq29/20); gHftgd66 = probit(freq30/20);
   gFftgd83 = probit(freq31/20); gHftgd83 = probit(freq32/20);
   gDbgbd66 = gHbgbd66 - gFbgbd66; gDbggd66 = gHbggd66 - gFbggd66;
   gDftbd66 = gHftbd66 - gFftbd66; gDftgd66 = gHftgd66 - gFftgd66;
   gDbgbd83 = gHbgbd83 - gFbgbd83; gDbggd83 = gHbggd83 - gFbggd83;
   gDftbd83 = gHftbd83 - gFftbd83; gDftgd83 = gHftgd83 - gFftgd83;
   BDbgbd66 = BHbgbd66 - BFbgbd66; BDbggd66 = BHbggd66 - BFbggd66;
   BDftbd66 = BHftbd66 - BFftbd66; BDftgd66 = BHftgd66 - BFftgd66;
   BDbgbd83 = BHbgbd83 - BFbgbd83; BDbggd83 = BHbggd83 - BFbggd83;
   BDftbd83 = BHftbd83 - BFftbd83; BDftgd83 = BHftgd83 - BFftgd83;
   gbgAtt66 = gDbggd66 - gDbgbd66; gftAtt66 = gDftgd66-gDftbd66;
   BbgAtt66 = BDbggd66 - BDbgbd66; BftAtt66 = BDftgd66-BDftbd66;
   gbgAtt83 = gDbggd83 - gDbgbd83; gftAtt83 = gDftgd83-gDftbd83;
   BbgAtt83 = BDbggd83 - BDbgbd83; BftAtt83 = BDftgd83-BDftbd83;
   gftbg66 = gftAtt66 - gbgAtt66; Bftbg66 = BftAtt66 - BbgAtt66;
   gftbg83 = gftAtt83 - gbgAtt83; Bftbg83 = BftAtt83 - BbgAtt83;
   Fruit = mean (gftAtt66, BftAtt66, gftAtt83, BftAtt83);
   Bugs = mean(gbgAtt66, BbgAtt66, gbgAtt83, BbgAtt83);
   FrtBgAAT = Fruit-Bugs;
   if mod(sub,2) = 1 then order = 1; else order = 0;

   BFbgbdal = probit((freq1+freq3)/40);  BHbgbdal = probit((freq2+freq4)/40);
   BFbggdal = probit((freq5+freq7)/40);  BHbggdal = probit((freq6+freq8)/40);
   BFftbdal = probit((freq9+freq11)/40); BHftbdal = probit((freq10+freq12)/40);
   BFftgdal = probit((freq13+freq15)/40); BHftgdal = probit((freq14+freq16)/40);
   gFbgbdal = probit((freq17+freq19)/40);  gHbgbdal = probit((freq18+freq20)/40);
   gFbggdal = probit((freq21+freq23)/40);  gHbggdal = probit((freq22+freq24)/40);
   gFftbdal = probit((freq25+freq27)/40); gHftbdal = probit((freq26+freq28)/40);
   gFftgdal = probit((freq29+freq31)/40); gHftgdal = probit((freq30+freq32)/40);
   bDbgbdal = bHbgbdal - bFbgbdal;   bDbggdal = bHbggdal - bFbggdal;
   bDftbdal = bHftbdal - bFftbdal;   bDftgdal = bHftgdal - bFftgdal;
   bFruitAl = bDftgdal - bDftbdal;   bBugsAl = bDbggdal - bDbgbdal;
   gDbgbdal = gHbgbdal - gFbgbdal;   gDbggdal = gHbggdal - gFbggdal;
   gDftbdal = gHftbdal - gFftbdal;   gDftgdal = gHftgdal - gFftgdal;
   gFruitAl = gDftgdal - gDftbdal;   gBugsAl = gDbggdal - gDbgbdal;

   bZbgbd83 = probit(1 - freq1/20); bZbgbd66 = probit(1 - freq3/20);
   gZbgbd83 = probit(1 - freq17/20); gZbgbd66 = probit(1 - freq19/20);
   bZbggd83 = probit(1 - freq5/20); bZbggd66 = probit(1 - freq7/20);
   gZbggd83 = probit(1 - freq21/20); gZbggd66 = probit(1 - freq23/20);
   bZftbd83 = probit(1 - freq9/20); bZftbd66 = probit(1 - freq11/20);
   gZftbd83 = probit(1 - freq25/20); gZftbd66 = probit(1 - freq27/20);
   bZftgd83 = probit(1 - freq13/20); bZftgd66 = probit(1 - freq15/20);
   gZftgd83 = probit(1 - freq29/20); gZftgd66 = probit(1 - freq31/20);

   bZbgbdAl = probit(1 - ((freq1+freq3)/40)); bZbggdal = probit(1 - ((freq5+freq7)/40));
   bZftbdal = probit(1 - ((freq9+freq11)/40)); bZftgdal = probit(1 - ((freq13+freq15)/40));
   gZbgbdAl = probit(1 - ((freq17+freq19)/40)); gZbggdal = probit(1 - ((freq21+freq23)/40));
   gZftbdal = probit(1 - ((freq25+freq27)/40)); gZftgdal = probit(1 - ((freq29+freq31)/40));

   bBbgbd83 = probnorm(bZbgbd83 - bDbgbd83)/probnorm(bZbgbd83);
   gBbgbd83 = probnorm(gZbgbd83 - gDbgbd83)/probnorm(gZbgbd83);
   bBbgbd66 = probnorm(bZbgbd66 - bDbgbd66)/probnorm(bZbgbd66);
   gBbgbd66 = probnorm(gZbgbd66 - gDbgbd66)/probnorm(gZbgbd66);
   bBbggd83 = probnorm(bZbggd83 - bDbggd83)/probnorm(bZbggd83);
   gBbggd83 = probnorm(gZbggd83 - gDbggd83)/probnorm(gZbggd83);
   bBbggd66 = probnorm(bZbggd66 - bDbggd66)/probnorm(bZbggd66);
   gBbggd66 = probnorm(gZbggd66 - gDbggd66)/probnorm(gZbggd66);
   bBftbd83 = probnorm(bZftbd83 - bDftbd83)/probnorm(bZftbd83);
   gBftbd83 = probnorm(gZftbd83 - gDftbd83)/probnorm(gZftbd83);
   bBftbd66 = probnorm(bZftbd66 - bDftbd66)/probnorm(bZftbd66);
   gBftbd66 = probnorm(gZftbd66 - gDftbd66)/probnorm(gZftbd66);
   bBftgd83 = probnorm(bZftgd83 - bDftgd83)/probnorm(bZftgd83);
   gBftgd83 = probnorm(gZftgd83 - gDftgd83)/probnorm(gZftgd83);
   bBftgd66 = probnorm(bZftgd66 - bDftgd66)/probnorm(bZftgd66);
   gBftgd66 = probnorm(gZftgd66 - gDftgd66)/probnorm(gZftgd66);

   bBbgbdal = probnorm(bZbgbdal - bDbgbdal)/probnorm(bZbgbdal);
   bBbggdal = probnorm(bZbggdal - bDbggdal)/probnorm(bZbggdal);
   bBftbdal = probnorm(bZftbdal - bDftbdal)/probnorm(bZftbdal);
   bBftgdal = probnorm(bZftgdal - bDftgdal)/probnorm(bZftgdal);
   gBbgbdal = probnorm(gZbgbdal - gDbgbdal)/probnorm(gZbgbdal);
   gBbggdal = probnorm(gZbggdal - gDbggdal)/probnorm(gZbggdal);
   gBftbdal = probnorm(gZftbdal - gDftbdal)/probnorm(gZftbdal);
   gBftgdal = probnorm(gZftgdal - gDftgdal)/probnorm(gZftgdal);
/*
proc print;var sub gDbgbd66 gDbggd66 gDftbd66 gDftgd66 gDbgbd83 gDbggd83 gDftbd83 gDftgd83;run;
proc print;var sub BDbgbd66 BDbggd66 BDftbd66 BDftgd66 BDbgbd83 BDbggd83 BDftbd83 BDftgd83;run;
*/
/*overall analysis*/
proc means; var bDbgbdal bdbggdal bdftbdal bdftgdal gDbgbdal gdbggdal gdftbdal gdftgdal;run;
proc glm; model bDbgbdal bdbggdal bdftbdal bdftgdal gDbgbdal gdbggdal gdftbdal gdftgdal = /nouni;
          repeated backgrnd 2, bugfruit 2, badgood 2;run;
proc glm; model bDbgbdal bdbggdal gDbgbdal gdbggdal = /nouni;
          repeated backgrnd 2, badgood 2;run;
proc glm; model bdftbdal bdftgdal gdftbdal gdftgdal = /nouni;
          repeated backgrnd 2, badgood 2;run;

proc glm; model bDbgbdal bdbggdal = /nouni; repeated badgood 2;title 'bugs only';run;
proc glm; model gDbgbdal gDbggdal = /nouni; repeated badgood 2;title 'bugs only';run;
proc glm; model bDftbdal bDftgdal = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model gDftbdal gDftgdal = /nouni;repeated badgood 2;title 'fruit only';run;

proc glm; model gDbgbd66 gDbggd66 gDftbd66 gDftgd66 gDbgbd83 gDbggd83 gDftbd83 gDftgd83
      BDbgbd66 BDbggd66 BDftbd66 BDftgd66 BDbgbd83 BDbggd83 BDftbd83 BDftgd83 = /nouni;
     repeated backgrnd 2, window 2, bugfruit 2, badgood 2;run;

/*bias analysis
proc means; var bbbgbdal bbbggdal bbftbdal bbftgdal gbbgbdal gbbggdal gbftbdal gbftgdal;run;
proc glm; model bbbgbdal bbbggdal bbftbdal bbftgdal gbbgbdal gbbggdal gbftbdal gbftgdal = /nouni;
          repeated backgrnd 2, bugfruit 2, badgood 2;run;
proc glm; model bbbgbdal bbbggdal = /nouni; repeated badgood 2;title 'bugs only';run;
proc glm; model gbbgbdal gbbggdal = /nouni; repeated badgood 2;title 'bugs only';run;
proc glm; model bbftbdal bbftgdal = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model gbftbdal gbftgdal = /nouni;repeated badgood 2;title 'fruit only';run;

proc glm; model gbbgbd66 gbbggd66 gbftbd66 gbftgd66 gbbgbd83 gbbggd83 gbftbd83 gbftgd83
      Bbbgbd66 Bbbggd66 Bbftbd66 Bbftgd66 Bbbgbd83 Bbbggd83 Bbftbd83 Bbftgd83 = /nouni;
     repeated backgrnd 2, window 2, bugfruit 2, badgood 2;run;
proc means;
  var gbbgbd66 gbbggd66 gbftbd66 gbftgd66 gbbgbd83 gbbggd83 gbftbd83 gbftgd83
      Bbbgbd66 Bbbggd66 Bbftbd66 Bbftgd66 Bbbgbd83 Bbbggd83 Bbftbd83 Bbftgd83;run;

proc glm; model gbbgbd66 gbbggd66 = /nouni;repeated badgood 2;title 'bugs only';run;
proc glm; model gbbgbd83 gbbggd83 = /nouni;repeated badgood 2;title 'bugs only';run;
proc glm; model Bbbgbd66 Bbbggd66 = /nouni;repeated badgood 2;title 'bugs only';run;
proc glm; model Bbbgbd83 Bbbggd83 = /nouni;repeated badgood 2;title 'bugs only';run;

proc glm; model gbftbd66 gbftgd66 = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model gbftbd83 gbftgd83 = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model Bbftbd66 Bbftgd66 = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model Bbftbd83 Bbftgd83 = /nouni;repeated badgood 2;title 'fruit only';run; */

/* sensitivity analysis

proc means;
  var gDbgbd66 gDbggd66 gDftbd66 gDftgd66 gDbgbd83 gDbggd83 gDftbd83 gDftgd83
      BDbgbd66 BDbggd66 BDftbd66 BDftgd66 BDbgbd83 BDbggd83 BDftbd83 BDftgd83;run;
proc print;var sub gDbgbd66 gDbggd66 gDftbd66 gDftgd66 gDbgbd83 gDbggd83 gDftbd83 gDftgd83;run;
proc print;var sub BDbgbd66 BDbggd66 BDftbd66 BDftgd66 BDbgbd83 BDbggd83 BDftbd83 BDftgd83;run;

proc glm; model gDbgbd66 gDbggd66 gDftbd66 gDftgd66 = order /nouni;repeated bugfruit 2, badgood 2;run;
proc glm; model gDbgbd83 gDbggd83 gDftbd83 gDftgd83 = order /nouni;repeated bugfruit 2, badgood 2;run;
proc glm; model BDbgbd66 BDbggd66 BDftbd66 BDftgd66 = order /nouni;repeated bugfruit 2, badgood 2;run;
proc glm; model BDbgbd83 BDbggd83 BDftbd83 BDftgd83 = order /nouni;repeated bugfruit 2, badgood 2;run;

proc glm; model gDbgbd66 gDbggd66 = /nouni;repeated badgood 2;title 'bugs only';run;
proc glm; model gDbgbd83 gDbggd83 = /nouni;repeated badgood 2;title 'bugs only';run;
proc glm; model BDbgbd66 BDbggd66 = /nouni;repeated badgood 2;title 'bugs only';run;
proc glm; model BDbgbd83 BDbggd83 = /nouni;repeated badgood 2;title 'bugs only';run;

proc glm; model gDftbd66 gDftgd66 = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model gDftbd83 gDftgd83 = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model BDftbd66 BDftgd66 = /nouni;repeated badgood 2;title 'fruit only';run;
proc glm; model BDftbd83 BDftgd83 = /nouni;repeated badgood 2;title 'fruit only';run;


/*
proc corr; var fruitbug mathread fruitgd fruitbd gftbg66 Bftbg66 gftbg83 Bftbg83 FrtBgAAT;
proc corr; var gftAtt66 BftAtt66 gftAtt83 BftAtt83 Fruit gbgAtt66 BbgAtt66 gbgAtt83 BbgAtt83 Bugs;
           with fruitbug mathread fruitgd fruitbd;run;
proc print; var sub fruitbug fruitgd fruitbd gftbg66 Bftbg66 gftbg83 Bftbg83 FrtBgAAT ;
proc print; var gftAtt66 BftAtt66 gftAtt83 BftAtt83 Fruit gbgAtt66 BbgAtt66 gbgAtt83 BbgAtt83 Bugs;run;
*/

/*
proc corr; var gbgAtt66 gbgAtt83 BbgAtt66 BbgAtt83 gftAtt66 gftAtt83 BftAtt66 BftAtt83;run;
proc corr; var Afrtbug Afruit Abugs;
  with gbgAtt66 gbgAtt83 BbgAtt66 BbgAtt83 gftAtt66 gftAtt83 BftAtt66 BftAtt83;run;
*/
